package cn.cs.service.impl;

import cn.cs.client.*;
import cn.cs.common.*;
import cn.cs.pojo.*;
import cn.cs.service.RepOrderService;
import cn.cs.service.RepairOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RepairOrderServiceImpl implements RepairOrderService {

    @Resource
    private RestRepairOrderClient restRepairOrderClient;

    @Resource
    private RepOrderService repOrderService;

    @Resource
    private RestClientUserClient clientUserClient;

    @Resource
    private AccountDetailClient accountDetailClient;

    @Resource
    private UserBalanceClient userBalanceClient;

    @Resource
    private RestRepairUserClient repairUserClient;

    @Resource
    private CsProportionSettingClient proportionSettingClient;

    @Resource
    private AgentClient agentClient;

    @Override
    public List<Map<String, Object>> getAppOrderByOrderState(String orderState) throws Exception {
        //根据订单状态获取预约单和订单信息
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();

        List<Map<String, Object>> appOrderInfo = restRepairOrderClient.getAppOrderByOrderState(orderState);
        map.put("appOrderInfo", appOrderInfo);

        List<Map<String, Object>> repOrderInfo = restRepairOrderClient.getRepairOrderByOrderState(orderState);
        map.put("repOrderInfo", repOrderInfo);
        list.add(map);

        return list;
    }

    @Override
    public List<String> getUserOrderType(String userId) throws Exception {
        return restRepairOrderClient.getUserOrderType(userId);
    }

    @Override
    public List<Map<String, Object>> getAppOrderByUserId(String userId) throws Exception {
        return restRepairOrderClient.getAppOrderByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> getAppOrderById(Map<String, Object> map) throws Exception {
        List<Map<String, Object>> list = restRepairOrderClient.getAppOrderByMap(map);
        for(Map<String, Object> a:list){
            String orderId = String.valueOf(a.get("orderId"));
            List<AfterOrder> afterOrderList = restRepairOrderClient.getAfterOrderByOrderId(orderId);
            if(afterOrderList.size() > 0){
                String state = afterOrderList.get(0).getState();
                if("0".equals(state) || "1".equals(state)){
                    a.put("afterSale", "1");//存在未处理的售后
                }else{
                    a.put("afterSale", "2");//存在售后，但是已经处理完毕
                }
            }else{
                a.put("afterSale", "0"); //未发起售后
            }
        }
        return list;
    }

    @Override
    public List<RepairOrder> getRepairOrderList(Map<String, Object> param) throws Exception {
        return restRepairOrderClient.getRepairOrderList(param);
    }

    @Override
    public List<RepairOrder> getRepairOrderListByMap(Map<String, Object> param) throws Exception {
        return restRepairOrderClient.getRepairOrderListByMap(param);
    }

    @Override
    public Integer getRepairOrderCountByMap(Map<String, Object> param) throws Exception {
        return restRepairOrderClient.getRepairOrderCountByMap(param);
    }

    public Integer addRepairOrder(RepOrder repairOrder) throws Exception {
        return restRepairOrderClient.addRepairOrder(repairOrder);
    }


    @Override
    public Page<Map<String, Object>> queryRepairOrderPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = restRepairOrderClient.getRepairOrderCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Map<String, Object>> repairOrderList = restRepairOrderClient.getAppOrderAllByMap(param);
        for(Map<String, Object> a:repairOrderList){
            String areaId = a.get("areaId").toString();
            List<Agent> list = agentClient.getAgentByOnlyArea(areaId);
            for(Agent b:list){
                if("1".equals(b.getTypeId())){
                    a.put("aPerson", b.getPerson());
                    a.put("aPhone", b.getPhone());
                }else{
                    a.put("bPerson", b.getPerson());
                    a.put("bPhone", b.getPhone());
                }
            }
        }
        page.setRows(repairOrderList);
        return page;
    }

    @Override
    public Page<Map<String, Object>> queryRepairOrderRepairUserPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = restRepairOrderClient.getRepairOrderRepairUserCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Map<String, Object>> repairOrderList = restRepairOrderClient.getAppOrderAllRepairUserByMap(param);
        for(Map<String, Object> a:repairOrderList){
            String areaId = a.get("areaId").toString();
            String rep_user_id = a.get("rep_user_id").toString();
            RepairUser repairUser = repairUserClient.getRepairUserById(rep_user_id);
            if(EmptyUtils.isNotEmpty(repairUser)){
                a.put("phone", repairUser.getPhone());a.put("personName", repairUser.getPersonName());a.put("nickname", repairUser.getNickname());
            }

            List<Agent> list = agentClient.getAgentByOnlyArea(areaId);
            for(Agent b:list){
                if("1".equals(b.getTypeId())){
                    a.put("AID", b.getId());
                    a.put("aPerson", b.getPerson());
                    a.put("aPhone", b.getPhone());
                }else{
                    a.put("BID", b.getId());
                    a.put("bPerson", b.getPerson());
                    a.put("bPhone", b.getPhone());
                }
            }
        }

        page.setRows(repairOrderList);
        return page;
    }

    /**
     * 经销商查看抽成详情
     * @param param
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public Page<RepairOrder> queryRepairOrderAgentAPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        if("1".equals(param.get("typeId"))){
            param.put("agentAId", param.get("agentId"));
            Integer total = restRepairOrderClient.getAppOrderAgentACountByMap(param);
            pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
            pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
            Page page = new Page(pageNo, pageSize, total);
            param.put("beginPos", page.getBeginPos());
            param.put("pageSize", page.getPageSize());
            List<RepairOrder> repairOrderList = restRepairOrderClient.getAppOrderAgentAByMap(param);
            page.setRows(repairOrderList);
            return page;
        }else{
            param.put("agentBId", param.get("agentId"));
            Integer total = restRepairOrderClient.getAppOrderAgentBCountByMap(param);
            pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
            pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
            Page page = new Page(pageNo, pageSize, total);
            param.put("beginPos", page.getBeginPos());
            param.put("pageSize", page.getPageSize());
            List<RepairOrder> repairOrderList = restRepairOrderClient.getAppOrderAgentBByMap(param);
            page.setRows(repairOrderList);
            return page;
        }
    }

    @Override
    public Dto queryRepairOrderAgentProfitAll(Map<String, Object> param) throws Exception {

        String month = DateUtil.format(new Date(), "yyyy-MM");
        String cycle = month + "-01 00:00:01";
        param.put("cycle", cycle);
        param.put("isAppset", "0");
        Map<String, Object> map = new HashMap<>();
        map.put("month", DateUtil.getLastMonth(1));
        if("1".equals(param.get("typeId"))){
            param.put("agentAId", param.get("agentId"));
            param.put("is_appsetA", "0");
            Double total = restRepairOrderClient.getAppOrderAgentAProfitAll(param);
            List<RepairOrder> repairOrderList = restRepairOrderClient.getAppOrderAgentAByMap(param);
            map.put("total", new BigDecimal(total).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            map.put("list", repairOrderList);
            return DtoUtil.returnSuccess("查询成功", map);
        }else{
            param.put("agentBId", param.get("agentId"));
            param.put("is_appsetB", "0");
            Double total = restRepairOrderClient.getAppOrderAgentBProfitAll(param);
            List<RepairOrder> repairOrderList = restRepairOrderClient.getAppOrderAgentBByMap(param);
            map.put("total", new BigDecimal(total).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
            map.put("list", repairOrderList);
            return DtoUtil.returnSuccess("查询成功", map);
        }

    }


    @Override
    public Dto updateRepairOrder(HttpServletRequest request, String id, String orderState) throws Exception {

        RepairOrder repairOrder = new RepairOrder();
        repairOrder.setId(id);
        repairOrder.setOrderState(orderState);
        restRepairOrderClient.updateRepairOrder(repairOrder);

        return DtoUtil.returnSuccess();
    }

    @Override
    @Transactional
    public Dto userBalancePay(String orderNo, String userId, double countSum) throws Exception {
        //修改账户余额
        ClientUser clientUser = clientUserClient.getClientUserById(userId);
        Double balance = clientUser.getBalance();
        if (balance > countSum) {
            Map<String, Object> order = restRepairOrderClient.getRepairOrderById(orderNo);
            String orderState = order.get("order_state").toString();
            if("2".equals(orderState) ){
                //修改订单状态

                    //根据订单查找预约单Id，修改预约单状态
                    int count2 = restRepairOrderClient.updateAppOrderStatus(orderNo, "4");
                    if (count2 > 0) {
                        //插入账户变动信息
                        AccountDetail accountDetail = new AccountDetail();
                        accountDetail.setId(IdWorker.getId());
                        accountDetail.setAmount(-countSum);
                        accountDetail.setTitle("支付维修费");
                        accountDetail.setTransactionTime(new Date());
                        accountDetail.setTransactionType(2);
                        accountDetail.setUpdatedTime(new Date());
                        accountDetail.setUserId(userId);
                        if (accountDetailClient.addAccountDetail(accountDetail) > 0) {

                            //可以支付
                            int count3 = userBalanceClient.updateUserBalanceNew(userId, String.valueOf(countSum));
                            if (count3 > 0) {



                                RepairOrder repairOrder = restRepairOrderClient.getRepairsOrderById(orderNo);
                                String rep_user_id = repairOrder.getRepUserId();

                                //修改维修工接单数量
                                repairUserClient.updateUserOrderNumber(rep_user_id);

                                List<Map<String, Object>> mapList = restRepairOrderClient.getOrderByRepUserId(rep_user_id);
                                //mapList <= 0 维修工 当天第一次被支付订单
                                String key = "userPayToRepairUser";
                                String val = "0";
                                if(mapList.size() <= 0){
                                    CsProportionSetting map1 = proportionSettingClient.getCsProportionSettingById("1");
                                    Double insurancePremium = Double.valueOf(map1.getInsurancePremium());
                                    Double proportionPlatform = Double.valueOf(map1.getProportionPlatform());
                                    Double proportionProvince = Double.valueOf(map1.getProportionProvince());
                                    Double proportionCity = Double.valueOf(map1.getProportionCity());
                                    Double all_cost = Double.valueOf(order.get("all_cost").toString());
                                    Double tax = Double.valueOf(order.get("tax").toString());
                                    Double earning = all_cost - tax;//先扣除税费
                                    earning = earning - insurancePremium; //再扣除保险费
                                    proportionPlatform = proportionPlatform * earning;
                                    proportionProvince = proportionProvince * earning;
                                    proportionCity = proportionCity * earning;

                                    earning = earning - proportionPlatform - proportionProvince - proportionCity;

                                    Map<String, Object> map2 = new HashMap<>();
                                    map2.put("all_cost", order.get("all_cost").toString());
                                    map2.put("earPlatform", new BigDecimal(proportionPlatform).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                                    map2.put("earProvince", new BigDecimal(proportionProvince).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                                    map2.put("earCity", new BigDecimal(proportionCity).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                                    map2.put("earning", new BigDecimal(earning).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                                    map2.put("insurance_premium", map1.getInsurancePremium());
                                    map2.put("rep_order_id", orderNo);
                                    map2.put("order_state", "3");
                                    map2.put("peopleCost", order.get("peopleCost").toString());

                                    restRepairOrderClient.updateRepOrderStatusMoney(map2);
                                    key = "repairUserInsurancePremium";
                                    val = map1.getInsurancePremium();
                                }else{
                                    RepOrder repOrder = new RepOrder();
                                    repOrder.setId(orderNo);
                                    repOrder.setPayTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                                    repOrder.setOrderState("3");
                                    repOrder.setPayType("1");
                                    restRepairOrderClient.updateRapairOrderStatus(repOrder);
                                }

                                // 发送极光推送给维修工
                                String[] strSelect = new String[1];
                                Map<String, String> cmap = new HashMap<String, String>();
                                cmap.put(key, val);
                                strSelect[0] = rep_user_id;
                                Object obj = JPushUtil.testSendPush(strSelect, "用户支付订单成功，请查看", cmap);
                                System.out.println(obj);

                                return DtoUtil.returnSuccess("支付成功", "true");

                            } else {
                                return DtoUtil.returnFail("支付失败", "false");
                            }

                        } else {
                            return DtoUtil.returnFail("插入交易信息失败", "false");
                        }

                    } else {
                        return DtoUtil.returnFail("修改余额信息或者修改预约单状态失败", "false");
                    }
//                } else {
//                    return DtoUtil.returnFail("修改失败", "false");
//                }
            }else{
                return DtoUtil.returnFail("已支付，请勿重复支付", "false");
            }
        } else {
            //余额不足
            return DtoUtil.returnFail("余额不足", "false");
        }

    }


    /**
     * 电器厂pc -- 余额支付
     * @param orderNo
     * @param userId
     * @param countSum
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Dto userBalancePayDQC(String orderNo, String userId, double countSum, String factoryId) throws Exception {
        //修改账户余额
        ClientUser clientUser = clientUserClient.getClientUserById(factoryId);
        Double balance = clientUser.getBalance();
        if (balance > countSum) {
            Map<String, Object> order = restRepairOrderClient.getRepairOrderById(orderNo);
            String orderState = order.get("order_state").toString();
            if("2".equals(orderState) ){

                //根据订单查找预约单Id，修改预约单状态
                int count2 = restRepairOrderClient.updateAppOrderStatus(orderNo, "4");
                if (count2 > 0) {
                    //插入账户变动信息
                    AccountDetail accountDetail = new AccountDetail();
                    accountDetail.setId(IdWorker.getId());
                    accountDetail.setAmount(-countSum);
                    accountDetail.setTitle("支付维修费");
                    accountDetail.setTransactionTime(new Date());
                    accountDetail.setTransactionType(2);
                    accountDetail.setUpdatedTime(new Date());
                    accountDetail.setUserId(factoryId);
                    accountDetail.setOperator(userId);
                    if (accountDetailClient.addAccountDetail(accountDetail) > 0) {

                        //可以支付
                        int count3 = userBalanceClient.updateUserBalanceNew(factoryId, String.valueOf(countSum));
                        if (count3 > 0) {

                            RepairOrder repairOrder = restRepairOrderClient.getRepairsOrderById(orderNo);
                            String rep_user_id = repairOrder.getRepUserId();

                            //修改维修工接单数量
                            repairUserClient.updateUserOrderNumber(rep_user_id);

                            List<Map<String, Object>> mapList = restRepairOrderClient.getOrderByRepUserId(rep_user_id);
                            //mapList <= 0 维修工 当天第一次被支付订单
                            String key = "userPayToRepairUser";
                            String val = "0";
                            if(mapList.size() <= 0){
                                CsProportionSetting map1 = proportionSettingClient.getCsProportionSettingById("1");
                                Double insurancePremium = Double.valueOf(map1.getInsurancePremium());
                                Double proportionPlatform = Double.valueOf(map1.getProportionPlatform());
                                Double proportionProvince = Double.valueOf(map1.getProportionProvince());
                                Double proportionCity = Double.valueOf(map1.getProportionCity());
                                Double all_cost = Double.valueOf(order.get("all_cost").toString());
                                Double tax = Double.valueOf(order.get("tax").toString());
                                Double earning = all_cost - tax;//先扣除税费
                                earning = earning - insurancePremium; //再扣除保险费
                                proportionPlatform = proportionPlatform * earning;
                                proportionProvince = proportionProvince * earning;
                                proportionCity = proportionCity * earning;

                                earning = earning - proportionPlatform - proportionProvince - proportionCity;

                                Map<String, Object> map2 = new HashMap<>();
                                map2.put("all_cost", order.get("all_cost").toString());
                                map2.put("earPlatform", new BigDecimal(proportionPlatform).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                                map2.put("earProvince", new BigDecimal(proportionProvince).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                                map2.put("earCity", new BigDecimal(proportionCity).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                                map2.put("earning", new BigDecimal(earning).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                                map2.put("insurance_premium", map1.getInsurancePremium());
                                map2.put("rep_order_id", orderNo);
                                map2.put("order_state", "3");
                                map2.put("peopleCost", order.get("peopleCost").toString());

                                restRepairOrderClient.updateRepOrderStatusMoney(map2);
                                key = "repairUserInsurancePremium";
                                val = map1.getInsurancePremium();
                            }else{
                                RepOrder repOrder = new RepOrder();
                                repOrder.setId(orderNo);
                                repOrder.setPayTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                                repOrder.setOrderState("3");
                                repOrder.setPayType("1");
                                restRepairOrderClient.updateRapairOrderStatus(repOrder);
                            }

                            // 发送极光推送给维修工
                            String[] strSelect = new String[1];
                            Map<String, String> cmap = new HashMap<String, String>();
                            cmap.put(key, val);
                            strSelect[0] = rep_user_id;
                            Object obj = JPushUtil.testSendPush(strSelect, "用户支付订单成功，请查看", cmap);
                            System.out.println(obj);

                            return DtoUtil.returnSuccess("支付成功", "true");

                        } else {
                            return DtoUtil.returnFail("支付失败", "false");
                        }

                    } else {
                        return DtoUtil.returnFail("插入交易信息失败", "false");
                    }

                } else {
                    return DtoUtil.returnFail("修改余额信息或者修改预约单状态失败", "false");
                }
//                } else {
//                    return DtoUtil.returnFail("修改失败", "false");
//                }
            }else{
                return DtoUtil.returnFail("已支付，请勿重复支付", "false");
            }
        } else {
            //余额不足
            return DtoUtil.returnFail("余额不足", "false");
        }

    }



    @Override
    @Transactional
    public Dto userBalancePayTestReport(String orderNo, String userId, double countSum) throws Exception {
        //修改账户余额
        ClientUser clientUser = clientUserClient.getClientUserById(userId);
        Double balance = clientUser.getBalance();
        if (balance > countSum) {
            Map<String, Object> map = restRepairOrderClient.getTestReportById(orderNo);
            String orderState = map.get("pay_status").toString();
            if("1".equals(map.get("order_state").toString()) || "2".equals(map.get("order_state").toString())){
                if("0".equals(orderState) ){
                    //插入账户变动信息
                    AccountDetail accountDetail = new AccountDetail();
                    accountDetail.setId(IdWorker.getId());
                    accountDetail.setAmount(-countSum);
                    accountDetail.setTitle("支付检测费");
                    accountDetail.setTransactionTime(new Date());
                    accountDetail.setTransactionType(2);
                    accountDetail.setUpdatedTime(new Date());
                    accountDetail.setUserId(userId);
                    if (accountDetailClient.addAccountDetail(accountDetail) > 0) {

                        //可以支付
                        int count3 = userBalanceClient.updateUserBalanceNew(userId, String.valueOf(countSum));
                        if (count3 > 0) {
                            int i = restRepairOrderClient.updateTestReportStatus(map.get("rep_order_id").toString(),map.get("app_order_id").toString(),map.get("id").toString());

                            String rep_user_id = map.get("rep_user_id").toString();


                            //经销商平台抽成   --- 暂时不要，屏蔽
                            Double testCost = Double.valueOf(map.get("testCost").toString());
                            CsProportionSetting map1 = proportionSettingClient.getCsProportionSettingById("1");
                            Double insurancePremium = Double.valueOf(map1.getInsurancePremium());
                            Double proportionPlatform = Double.valueOf(map1.getProportionPlatform());
                            Double proportionProvince = Double.valueOf(map1.getProportionProvince());
                            Double proportionCity = Double.valueOf(map1.getProportionCity());

                            proportionPlatform = proportionPlatform * testCost;
                            proportionProvince = proportionProvince * testCost;
                            proportionCity = proportionCity * testCost;
                            List<Map<String, Object>> mapList = restRepairOrderClient.getOrderByRepUserId(rep_user_id);
                            //mapList <= 0 维修工 当天第一次被支付订单
                            String key = "userPayTestReportToRepairUser";
                            String val = "0";
                            Double earning = testCost;
                            Map<String, Object> map2 = new HashMap<>();
                            if(mapList.size() <= 0){
                                earning = earning - insurancePremium;
                                key = "testReportToRepairUserIP";
                                map2.put("insurance_premium", map1.getInsurancePremium());
                                val = map1.getInsurancePremium();
                            }else{
                                map2.put("insurance_premium", "0");
                            }


                            map2.put("all_cost", map.get("testCost"));
                            map2.put("earPlatform", "0");
                            map2.put("earProvince", "0");
                            map2.put("earCity", "0");
                            map2.put("earning", earning);

                            map2.put("order_state", "6");
                            map2.put("peopleCost", map.get("testCost"));
                            map2.put("rep_order_id",map.get("rep_order_id").toString());
                            i = restRepairOrderClient.updateRepOrderStatusMoney(map2);





                            // 发送极光推送给维修工
                            String[] strSelect = new String[1];
                            Map<String, String> cmap = new HashMap<String, String>();
                            cmap.put(key, val);
                            strSelect[0] = map.get("rep_user_id").toString();
                            Object obj = JPushUtil.testSendPush(strSelect, "用户支付检测费成功，请查看", cmap);
                            System.out.println(obj);

                            return DtoUtil.returnSuccess("支付成功", i);
                        } else {
                            return DtoUtil.returnFail("支付失败", "004");
                        }

                    } else {
                        return DtoUtil.returnFail("插入交易信息失败", "003");
                    }

                }else{
                    return DtoUtil.returnFail("已支付，请勿重复支付", "002");
                }
            }else{
                return DtoUtil.returnFail("订单不可结束，请刷新后查看订单信息", "005");
            }

        } else {
            //余额不足
            return DtoUtil.returnFail("余额不足", "001");
        }

    }

    @Override
    public Map<String, Object> getOrderDetailsByOrderId(String orderId) throws Exception {
        Map<String, Object> map = restRepairOrderClient.getBillOrderDetailsByOrderId(orderId);
        map.put("imgList", restRepairOrderClient.getBillOrderImgByOrderId(orderId));
        map.put("orderDetails", restRepairOrderClient.getOrderDetailsByOrderId(orderId, "2"));
        return map;
    }

    @Override
    public Integer updateOrderStatusByAppId(String order_state, String appId) {
        return restRepairOrderClient.updateOrderStatusByAppId(order_state, appId);
    }

    @Override
    public Map<String, Object> getOrderByAppOrderId(String appOrderId) {
        return restRepairOrderClient.getRepOrderByAppOrderId(appOrderId);
    }

    public static void main(String args[])throws  Exception{
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        System.out.println(dayWeek);

        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        // System.out.println(sdf.format(calendar1.getTime()));// last Monday
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String lastBeginDate = format.format(calendar1.getTime());
        // System.out.println(sdf.format(calendar2.getTime()));// last Sunday
        String lastEndDate = format.format(calendar2.getTime());
        System.out.println(lastBeginDate);
        System.out.println(lastEndDate);


        cal.setTime(format.parse("2019-05-13"));
        int dayWeek11 = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        System.out.println(dayWeek11);
    }

    public ClientUser getClientUserById(Long id)throws Exception{
        return restRepairOrderClient.getClientUserById(id);
    }

    public Map<String, Object> getTestReportById(String orderNo)throws Exception{
        return restRepairOrderClient.getTestReportById(orderNo);
    }

    @Override
    public Map<String, Object> queryRepairInsurancePageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Map<String, Object> repairMap = restRepairOrderClient.getInsurancePremiumCount(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);
        map.put("total", repairMap.get("val"));
        map.put("pageCount", (Integer.valueOf(repairMap.get("val").toString()) + pageSize - 1) /pageSize);
        map.put("beginPos", (pageNo-1)*pageSize);

        param.put("beginPos", map.get("beginPos"));
        param.put("pageSize", pageSize);
        List<Map<String, Object>> repairOrderList = restRepairOrderClient.getInsurancePremiumByMap(param);
        for(Map<String, Object> a:repairOrderList){
            String repairUserId = a.get("rep_user_id").toString();
            RepairUser repairUser = repairUserClient.getRepairUserById(repairUserId);
            a.put("nickname", repairUser.getNickname());a.put("personName", repairUser.getPersonName());a.put("phone", repairUser.getPhone());
        }
        map.put("rows", repairOrderList);
        map.put("allMoney", repairMap.get("allMoney"));
        return map;
    }



}
